<template>
  <el-dialog class="insuranceDialog" :visible.sync="visible" :title="dataForm.id?'编辑':'新增'" width="600px" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="车辆保险类型" prop="carInsuranceType">
        <el-input class="s_input" v-model="dataForm.carInsuranceType"></el-input>
      </el-form-item>
      <el-form-item label="保费规则" prop="premium">
        <quill-editor ref="text" v-model="dataForm.premium" class="s_input" :options="editorOption" />
      </el-form-item>
      <el-form-item label="理赔规则" prop="claimRules">
        <quill-editor ref="text" v-model="dataForm.claimRules" class="s_input" :options="editorOption" />
      </el-form-item>
    </el-form>
    <template slot="footer">
      <el-button size="small" @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button size="small" type="primary" @click="submit()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import debounce from 'lodash/debounce'
// 工具栏配置
const toolbarOptions = []
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      visible: false,
      dataForm: {},
      editorOption: {
        modules: {
          toolbar: {
            container: toolbarOptions // 工具栏
          }
        }
      }
    }
  },
  computed: {
    dataRule () {
      return {
        carInsuranceType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        premium: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        claimRules: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 显示弹窗
    init (row) {
      this.visible = true
      if (row) {
        this.dataForm = JSON.parse(JSON.stringify(row))
      } else {
        this.dataForm = {
          id: '',
          carInsuranceType: '',
          premium: '',
          claimRules: ''
        }
      }
      console.log(this.dataForm)
    },
    // 表单提交
    submit: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[this.dataForm.id ? 'put' : 'post']('/carinsurance', this.dataForm).then(({ data: res }) => {
          if (res.code === 0) {
            this.$message.success('保存成功!')
            this.visible = false
            this.$emit('refresh')
          } else {
            this.$message.error(res.msg)
          }
        }).catch((error) => {
          return this.$message.error(error)
        })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style lang="scss">
.insuranceDialog{
  .el-dialog__header{
    .el-dialog__title{
      font-size: 14px;
      font-weight: bold;
    }
    border-bottom: 1px solid #f2f2f2;
  }
  .el-dialog__body{
    padding: 15px 20px;
  }
  .s_input{
    width: 80%;
    margin-right: 10px;
  }
}
</style>
